package com.openbravo.data.loader.dialect.SQLite;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DBSession;
import com.openbravo.data.loader.dialect.AbstractDataBase;
import e.odbo.DB;

import java.io.File;
import java.sql.Driver;
import java.sql.DriverManager;

public class SQLite extends AbstractDataBase<SQLite> {
    public final static String JDBCDriver="org.sqlite.JDBC";

    public final static String NAME="SQLite";

    public final static String URLPREV="jdbc:sqlite:";

    public final static String SQLDroid_URLPREV="jdbc:sqldroid:";

    public final  static String SQLDroid_JDBCDriver="org.sqldroid.SQLDroidDriver";

    public SQLite() {
        super(NAME);
    }

    public static class TYPE{
    }

    public static DB embedded(String database, String user, String password)throws BasicException {
        String url = String.format(URLPREV+"%s",  database+".db");
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    public static DB embeddedDir(String path,String database,String user, String password)throws BasicException{
        /**
        Properties props=System.getProperties(); //获得系统属性集
        String osName = props.getProperty("os.name"); //操作系统名称
        osName = osName.toLowerCase();
        int index = 0;
        index = osName.indexOf("windows");
        if(index > -1){
            path = "/"+path.toLowerCase();
        }*/
        return embedded(path+"/"+database,user,password);
    }


    public static DB embeddedWeb(String database, String user, String password)throws BasicException {
        String url = String.format(URLPREV+":resource:%s",  database+".db");
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    public static DB embeddedWebDir(String path,String database, String user, String password)throws BasicException {
        return embeddedWeb(path+"/"+database,user,password);
    }

    //注：当connection.close();之后，之前的数据就会消失
    public static DB mem()throws BasicException{
        String url = URLPREV+":memory:";
        DBSession dbSession=new DBSession(url,null,null);
        return DB.getInstance(dbSession);
    }


    public static DB androidWithSQLDroid(String packageName,String database, String user, String password)throws BasicException {
        return androidExtWithSQLDroid("/data/data/" + packageName +"/databases",database,user,password);
    }

    public static DB androidExtWithSQLDroid(String dir,String database, String user, String password)throws BasicException {
        return androidExtWithSQLDroid(dir+"/"+database+".db",user,password);
    }

    public static DB androidExtWithSQLDroid(String fileWithPath, String user, String password)throws BasicException {
        try {
            DriverManager.registerDriver((Driver) Class.forName(SQLDroid_JDBCDriver).newInstance());
        } catch (Exception e) {
            throw new BasicException("Failed to register SQLDroidDriver");
        }

        File f = new File(fileWithPath);
        if (!f.exists() ) {
            if (null != f.getParent()) {
                f.getParentFile().mkdirs();
            }
        }
        String url = String.format(SQLDroid_URLPREV+"%s",fileWithPath);
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    public static void help(){
       /*'<dependencies>
            <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>(version)</version>
            </dependency>
        </dependencies>'*/
    }
}
